import datetime
import asyncio


# asyncio.run

#########
# 1. 同步执行io
# async def delay_print(delay, msg):
#     await asyncio.sleep(delay)
#     print(datetime.datetime.now(), msg)

# async def main():
#     await delay_print(1, 'hello')
#     await delay_print(2, 'world')

# print(datetime.datetime.now())
# asyncio.run(main())

##########

##########
# 2. 异步执行
async def delay_print(delay, msg):
    await asyncio.sleep(delay)
    print(datetime.datetime.now(), msg)

async def main():
    task1 = asyncio.create_task(coro=delay_print(delay=1, msg='hello'))
    task2 = asyncio.create_task(coro=delay_print(delay=2, msg='world'))
    
    print('start at: ', datetime.datetime.now())
    
    await task1
    await task2

    print('end at: ', datetime.datetime.now())

asyncio.run(main())
